const db = require('../db/index')
const bcrypt = require('bcryptjs')
const jwt = require("jsonwebtoken")
const config = require("../config.js")
// 导入处理路径的 path 核心模块
const path = require('path')
exports.uploadAvatar = (req, res) => {
  // return console.log(req.file);
  // 手动判断是否上传了文章封面
  if (!req.file || req.file.fieldname !== 'imageUrl') return res.cc('头像是必选参数！')
  const sql = `update users set ? where id = ?`;
  let articleInfo = {
    imageUrl: path.join('/uploads', req.file.filename)
  }
  // 执行 SQL 语句
  db.query(sql, [articleInfo, req.user.id], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err)

    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('头像设置失败！')


    // res.cc('', 0)
    res.send({
      // 状态
      status: 0,
      message: '头像设置成功',
      data: path.join('/uploads', req.file.filename)
    })
  })
}

exports.info = (req, res) => {
  const sql = "select * from users where id =?";
  db.query(sql, req.user.id, (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err)
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.length !== 1) return res.cc('获取数据失败！')
    res.send({
      // 状态
      status: 0,
      message: '获取数据成功',
      data: results[0]
    })
  })
}

exports.changePassword = (req, res) => {
  const sql = "select * from users where id = ?";
  db.query(sql, req.user.id, (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err)
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.length !== 1) return res.cc('获取数据失败！')
    const isTrue = bcrypt.compareSync(req.body.oldPassword, results[0].password);
    if (!isTrue) {
      return res.cc('原密码输入错误')
    }
    const sql1 = `update users set password =? where id = ?`;
    let password = bcrypt.hashSync(req.body.newPassword, 10);
    db.query(sql1, [password, req.user.id], (err, results1) => {
      // 执行 SQL 语句失败
      if (err) return res.cc(err)
      // 执行 SQL 语句成功，但是影响行数不等于 1
      if (results1.affectedRows !== 1) return res.cc('修改失败！')
      res.cc('修改成功', 0)
    })
  })
}

exports.changeName = (req, res) => {
  const sql = "update users set name = ? where id = ?";
  db.query(sql, [req.body.name, req.user.id], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err);
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('更新失败！');
    res.cc('更新成功', 0)
  })
}

exports.changeSex = (req, res) => {
  const sql = "update users set sex = ? where id = ?";
  db.query(sql, [req.body.sex, req.user.id], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err);
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('更新失败！');
    res.cc('更新成功', 0)
  })
}

exports.changeEmail = (req, res) => {
  const sql = "update users set email = ? where id = ?";
  db.query(sql, [req.body.email, req.user.id], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err);
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('更新失败！');
    res.cc('更新成功', 0)
  })
}

exports.addProductManager = (req, res) => {
  const sql = "select * from users where account = ?";
  db.query(sql, req.body.account, (err, results1) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err);
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results1.length == 1) return res.cc('用户已存在');
    const sql = "insert into users set ? ";
    let password = bcrypt.hashSync(req.body.password, 10);
    let info = {
      account: req.body.account,
      password: password,
      name: req.body.name,
      sex: req.body.sex,
      email: req.body.email,
      department: req.body.department,
      identity: req.body.identity
    }
    db.query(sql, info, (err, results) => {
      // 执行 SQL 语句失败
      if (err) return res.cc(err);
      // 执行 SQL 语句成功，但是影响行数不等于 1
      if (results.affectedRows !== 1) return res.cc('添加失败！');
      res.cc('添加成功', 0)
    })

  })


}

exports.getProductManager = (req, res) => {
  let pageNum = req.body.pageNum;
  let pageSize = req.body.pageSize;
  let smitsql = ``;
  if (req.body.account) {
    smitsql = `and account like '%${req.body.account}%'`;
  }
  let sql = `select * from users where identity = ? ` + smitsql + ` limit ${(pageNum - 1) * pageSize} ,${pageSize}`;

  db.query(sql, req.body.identity, (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err)

    let sql1 = `select count(*) as total from users where identity = ?` + smitsql;

    db.query(sql1, req.body.identity, (err, results1) => {
      // 执行 SQL 语句失败
      if (err) return res.cc(err)
      res.send({
        // 状态
        status: 0,
        message: '获取数据成功',
        data: {
          list: results,
          total: results1[0].total
        }
      })
    })

  })
}
exports.modifyProductManager = (req, res) => {
  let sql = "update users set ? where id = ?";
  let info = {
    // identity: req.body.identity,
    name: req.body.name,
    sex: req.body.sex,
    email: req.body.email,
    department: req.body.department
  }

  db.query(sql, [info, req.body.id], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err)
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('更新失败！');
    res.cc('更新成功', 0)
  })
}


exports.deleteProductManager = (req, res) => {
  let sql = "update users set identity = '用户' where id =?";

  db.query(sql, req.query.id, (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err)
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('更新失败！');
    res.cc('更新成功', 0)
  })
}


exports.getUser = (req, res) => {
  let pageNum = req.body.pageNum;
  let pageSize = req.body.pageSize;
  console.log(req.body.department, 'department');
  let sql = '';
  let department = req.body.department ? req.body.department : null
  console.log(req.body.department, department, 'department');
  let limitsql = ``;
  let departmentsql = ``;
  if (req.body.account) {
    limitsql = ` and account  like '%${req.body.account}%' `;
  }
  if (req.body.department) {
    departmentsql = ` and department = '${req.body.department}' `;
  }

  sql = `select * from users where status = ifnull(${req.body.type},status)  and identity ='用户' ` + departmentsql + limitsql + ` limit ${(pageNum - 1) * pageSize} ,${pageSize}`;

  db.query(sql, (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err)
    let sql1 = `select * from users where status = ifnull(${req.body.type},status)  and identity ='用户' ` + departmentsql + limitsql;

    db.query(sql1, (err, results1) => {
      // 执行 SQL 语句失败
      if (err) return res.cc(err)
      res.send({
        // 状态
        status: 0,
        message: '获取数据成功',
        data: {
          list: results,
          total: results1.length
        }
      })
    })

  })
}


exports.deletUser = (req, res) => {
  const sql = "delete from users  where id = ?";
  db.query(sql, [req.query.id], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err);
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('删除失败！');
    res.cc('删除成功', 0)
  })
}

exports.empowerment = (req, res) => {
  const sql = "update users set identity = ? where id = ?";
  db.query(sql, [req.body.identity, req.body.id], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err);
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('更新失败！');
    res.cc('更新成功', 0)
  })
}


exports.changeType = (req, res) => {
  const sql = "update users set status = ? where id = ?";
  db.query(sql, [req.body.type, req.body.id], (err, results) => {
    // 执行 SQL 语句失败
    if (err) return res.cc(err);
    // 执行 SQL 语句成功，但是影响行数不等于 1
    if (results.affectedRows !== 1) return res.cc('更改失败！');
    res.cc('更改成功', 0)
  })
}


